package com.sn.dao;

import com.sn.model.CustomerScore;
import com.sn.model.CustomerScoreLog;
import com.sn.model.req.CustomerScoreLogReq;
import com.sn.model.req.CustomerScoreReq;
import com.sn.model.vo.CustomerScoreLogVo;
import com.sn.model.vo.CustomerScoreVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * Created by qingshengzheng on 18/6/21.
 */
@Mapper
public interface ScoreMapper {

    @Insert("insert into customerscore(cid,mscore,tscore,createtime,updatetime) values(#{cid},#{mscore},#{tscore},#{createtime},#{updatetime})")
    public void addScore(CustomerScore score);

    @Insert("insert into scorelog(cid,mscore,tscore,tocid,createtime,orderid) values(#{cid},#{mscore},#{tscore},#{tocid},#{createtime},#{orderid})")
    public void addScoreLog(CustomerScoreLog log);

   @Select("<script>" +
           "select a.*,b.truename as truename,(select truename from customer as d where d.cid=a.tocid) as vipname,c.product,c.pnum from scorelog as a left join customer as b on a.cid=b.cid  left join chargeorder as c on a.orderid=c.orderid  " +
           "<where>" +
           "<if test=\"cid!=null and cid !=''\">and a.cid=#{cid}</if>" +
           "<if test=\"usertype!=null and usertype !=''\"> and b.usertype=#{usertype}</if>" +
           "<if test=\"truename!=null and truename !=''\"> and b.truename like  concat('%',#{truename},'%')</if>" +
           "<if test=\"begintime!=null and begintime !=''\"> and a.createtime &gt;= #{begintime}</if>" +
           "<if test=\"endtime!=null and endtime !=''\"> and a.createtime &lt; #{endtime}</if>" +
           "</where>"+
           "</script>")
    public List<CustomerScoreLogVo> queryScoreLogs(CustomerScoreLogReq log);


    @Update("<script>" +
            "update  customerscore" +
            "<set>" +
            "<if test=\"mscore!=null and mscore !=''\">mscore=mscore+#{mscore},</if>" +
            "<if test=\"tscore!=null and tscore !=''\">tscore=tscore+#{tscore},</if>" +
            "<if test=\"updatetime!=null and updatetime !=''\">updatetime=#{updatetime},</if>" +
            "</set>" +
            "where cid=#{cid}"+
            "</script>")
    public void increaseScore(CustomerScore score);
    @Select("select * from customerscore where cid=#{id}")
    public CustomerScoreVo scoreDetail(Long cid);


    @Select("select * from customerscre where 1=1")
    public List<CustomerScoreVo> queryScores(CustomerScoreReq req);

    @Select("<script>" +
            "select * from customerscore as a " +
            "<where>" +
            "<if test=\"cid!=null and cid !=''\">and a.cid=#{cid} </if>" +
            "<if test=\"orderid!=null and orderid !=''\">and a.orderid=#{orderid} </if>" +
            "</where>" +
            "</script>")
    public List<CustomerScoreVo> queryCustomerScore(CustomerScore score );


}
